using System;
using System.Collections;
using System.Web.UI.WebControls;
using THANHTUNGO.Portal.Components.Security;


namespace MONA.Modules.Marketing.Controls
{
    public partial class AddEditEmployee : AddEditItemControl
    {
        public event EventHandler SaveClick;

        public bool Save()
        {
            int returnId = 0;
            if (Page.IsValid)
            {
                    BLL.Employee employee = new BLL.Employee();
                    employee.ManagerID = Convert.ToInt32(ddlManager.SelectedValue);
                    employee.ContactID = Convert.ToInt32(ddlContact.SelectedValue);
                    employee.Location = txtLocation.Text.Trim();
                    employee.Type = txtType.Text.Trim();
                    employee.Description = txtDescription.Text.Trim();
                    employee.LoginID = txtLoginID.Text;
                    employee.Password = PortalSecurity.Encrypt(employee.Password);

                    if (this.ItemID == 0)
                    {
                        if (User.CheckEmail(txtLoginID.Text.Trim()) == false)
                        {
                            lblCheckLoginID.Text = txtLoginID.Text.Trim() + " is not available.";
                            return false;
                        }
                        else
                        {
                            returnId = employee.Insert();
                        }
                    }
                    else
                    {
                        employee.ID = this.ItemID;
                        employee.Update();
                    }

                    ArrayList roles = new ArrayList();
                    foreach (ListItem item in chkUserRole.Items)
                    {
                        if (item.Selected)
                        {
                            roles.Add(item.Value);
                        }
                    }

                    User.AddToRoles(employee.ID, roles);
            }
            return returnId > 0;
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            UpdateBar.Visible = this.ShowUpdateBar;
            btnDelete.Visible = this.ItemID > 0;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                chkUserRole.DataSource = Role.SelectAll();
                chkUserRole.DataBind();

                if (this.ItemID > 0)
                {
                    // Disable LoginID TextBox.
                    txtLoginID.Enabled = false;

                    // Hide Check Availability Button.
                    btnCheckLoginID.Visible = false;

                    BLL.Employee employee = BLL.Employee.Load(this.ItemID);
                    if (employee != null)
                    {
                        ddlManager.SelectedValue = employee.ManagerID.ToString();
                        ddlContact.SelectedValue = employee.ContactID.ToString();
                        txtLocation.Text = employee.Location;
                        txtType.Text = employee.Type;
                        txtDescription.Text = employee.Description;
                        txtLoginID.Text = employee.LoginID;

                        string[] roles = User.GetRoles("Email");
                        foreach (ListItem item in chkUserRole.Items)
                        {
                            foreach (string role in roles)
                            {
                                if (role == item.Value)
                                {
                                    item.Selected = true;
                                }
                            }
                        }
                    }
                }
            }
        }


        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.ReturnToReferrerPage();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            this.Save();
            if (SaveClick != null)
                SaveClick(sender, e);

            this.ReturnToReferrerPage();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.ItemID > 0)
            {
                BLL.Employee employee = new BLL.Employee();
                employee.ID = this.ItemID;
                employee.Delete();
                this.ReturnToReferrerPage();
            }
        }

        protected void AddEditContact_SaveClick(object sender, EventArgs e)
        {
            ddlContact.DataBind();
        }

        protected void btnCheckLoginID_Click(object sender, EventArgs e)
        {
            if (txtLoginID.Text.Trim().Length > 0)
            {
                if (User.CheckEmail(txtLoginID.Text.Trim()) == false)
                {
                    lblCheckLoginID.Text = txtLoginID.Text.Trim() + " is not available.";
                }
                else
                {
                    lblCheckLoginID.Text = txtLoginID.Text.Trim() + " is available.";
                }
            }
        }
    }
}